/**讲堂搜索入口
 * Created by alice on 2016/7/11.
 */
require.config({
    baseUrl:'../js',
    paths:{
        "getData":"Util/getData",
        "config":"common/config",
        "components":"common/components",
        "common":"common/common",
        "jquery":"lib/jquery.min",
        "wx":"lib/jweixin-1.0.0"
    },
    shim : {
        "components": { deps : ["jquery"] }
    },
    urlArgs: "v=" + (new Date()).getTime()//以禁用浏览器缓存,上线后注释掉
});
require(['jquery','getData','config','common','components','wx'],function($,getData,config,common,components,wx){
    //清除默认input焦点
    $(".receive_input input").blur();
    //获取gm obj
    var gm=common.gm;
    // 获取URL的参数
    var _p = gm.para.get(window.location.href,"name");
    var token=gm.para.get(window.location.href,"token");
    // 转码
    _p = decodeURIComponent(_p);
    $(".receive_input input").val(_p);
    // 加载后执行接口
    getData.getJson({
        page:"doctor",
        loading:true,
        inter:"getDoctorCommonVideoList",
        data:{"token":token,"pageSize":20,"videoName":_p},
        callback:function(data){
            getresult(data);
        }
    });

    //点击搜索按钮
    $(".start_find").bind({
        touchend:function(){
            //获取输入文字
            var _o = $(".receive_input input").val();
            //点击按钮后执行接口
            getData.getJson({
                page:"doctor",
                loading:true,
                inter:"getDoctorCommonVideoList",
                data:{"token":token,"pageSize":20,"videoName":_o},
                callback:function(data){
                    getresult(data);
                }
            });
        }
    });

    //接口回调
    function getresult(data){
        if(!data){
            components.alert("服务器出错!");
        }
        if(data.returnStatus!=undefined && !data.returnStatus){
            $("#search_empty").show();
        }else
            $("#search_empty").hide();
        if(data.code=="2000"){
            var arr = [];

            var tmp = '<li uuid="{uuid}" favid="{favoriteUuid}">\
                <a href="{url}">\
                    <div class="asbg">\
                        <div class="live">\
                            <img src="{image}">\
                        </div>\
                        <div class="text">\
                            <div class="list_title collectionContentTitle">\
                            <p>{userName}-{videoName}</p>\
                            </div>\
                            <p class="time">\
                                {commentNumber}\
                                {collNumber}\
                            </p>\
                        </div>\
                    </div>\
                </a>\
			    </li>';
            $(data.value).each(function(i,o){

                var imgstr=o.image;
                o.image = gm.common.isObjUsable(imgstr)?(imgstr.substr(0,7)=="http://"?imgstr:window._interfaceImgPath+""+imgstr):"/static/imgs/spimg.png";


                //是否收藏
                var isCollection=o["isCollect"] == 1?1:0;
                var title= o.videoName;//视频title
                var desc= o.videoIntroduction;//视频intr
                var img= o.img;//图像
                var favoriteUuid=o.id;//视频id

                var str="isCollection="+isCollection+"&title="+title+"&desc="+desc+"&img="+img+"&favoriteUuid="+favoriteUuid;

                o.url="playing.html?client=ios&token="+token+"&"+str;
                //o["sc"] = (o["sc"] == "1"?"redstars":"greystars");//redstars表示已经收藏 ，greystars表示 未收藏
                var commentNumber=gm.common.isObjUsable(o.commentNumber);
                o.commentNumber=commentNumber?"<span style='margin-right: .5rem;'>评论 "+o.commentNumber+"</span>":"<span style='margin-right: .5rem;'>评论 0</span>";//判断评论字段是否存在
                var collNumber=gm.common.isObjUsable(o.collNumber);
                o.collNumber=collNumber?"<span style='margin-right: .5rem;'>收藏 "+o.collNumber+"</span>":"<span style='margin-right: .5rem;'>收藏 0</span>";
                arr.push(gm.replace(tmp,o));
            });
            $(".ensss").html(arr.join(""));

        }else{
            components.alert(data.query.message);
        }

    }
    //页面后退
    gm.pul.returnurl = function(){
        gm.pul.toUrl('/html/lecture/all_course.shtml');
    };
    //防止表单提交
    $('form').on('submit', function(e){
        // 不提交
        return false;
    });
    setTimeout(function(){
        gm.common.setTitle(document.title);
    },200)
});
